const { Controller } = require('egg');
const validate = require('../rule/rule');

class HomeController extends Controller {

  /**
   *
   * @api { POST } /auth/code 获取验证码
   * @apiName code
   * @apiGroup auth
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String} visitor 游客id
   *
   * @apiHeader { String } Authorization 注册信息
   */

  async code(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { visitor } = validate.codeValidate(body);
      const data = await ctx.service.home.code(visitor);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   *
   * @api { POST } /auth/login 登录
   * @apiName login
   * @apiGroup auth
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } login_name 登录名
   * @apiParam { String } password 密码
   * @apiParam { String } code 图形验证码
   * @apiParam { String} visitor 游客id
   *
   * @apiHeader { String } Authorization 注册信息
   *
   */

  async login(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { login_name, password, code, visitor } = await validate.loginValidate(body);
      const data = await ctx.service.home.login(login_name, password, code, visitor);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   *
   * @api { POST } /auth/refresh 令牌刷新
   * @apiName refresh
   * @apiGroup auth
   * @apiVersion 1.0.0
   *
   *
   * @apiHeader { String } Authorization 注册信息
   *
   */

  async refresh(req, res) {
    const ctx = this.ctx;
    try {
      const { refresh_token } = req.headers;
      const data = await ctx.service.home.refresh(refresh_token);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   *
   * @api { POST } /auth/logout 用户登出
   * @apiName logout
   * @apiGroup auth
   * @apiVersion 1.0.0
   *
   *
   * @apiHeader { String } Authorization 注册信息
   *
   */

  async logout(req, res) {
    const ctx = this.ctx;
    try {
      const { refresh_token } = req.headers;
      const data = await ctx.service.home.logout(refresh_token);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 500;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  async checkout() {
    const ctx = this.ctx;
    try {
      const params = ctx.request.body;
      const { key, role_id } = await validate.checkoutValidate(params);
      const data = await ctx.service.home.checkout(key, role_id);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }
}

module.exports = HomeController;
